/*
Package auth
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/3/27 15:05
@File     : password_controller.go
@Project  : gohub
@Desc     : 处理密码重置
*/
package auth

import (
	"github.com/gin-gonic/gin"
	v1 "gohub/app/http/controllers/api/v1"
	"gohub/app/models/user"
	"gohub/app/requests"
	"gohub/pkg/response"
)

type PasswordController struct {
	v1.BaseAPIController
}

/**
 * ResetByPhone
 * @Description: 重置密码
 * @receiver pc
 * @param c
 */
func (pc *PasswordController) ResetByPhone(c *gin.Context) {
	//校验表单
	request := requests.ResetByPhoneRequest{}
	if ok := requests.Validate(c, &request, requests.ResetByPhone); !ok {
		return
	}
	//更新密码
	userModel := user.GetByPhone(request.Phone)
	if userModel.ID == 0 {
		response.Abort404(c)
	} else {
		userModel.Password = request.Password
		userModel.Save()

		response.Success(c)
	}
}

/**
 * ResetByEmail
 * @Description: 邮箱重置密码
 * @receiver pc
 * @param c
 */
func (pc *PasswordController) ResetByEmail(c *gin.Context) {
	//表单校验
	request := requests.ResetByEmailRequest{}
	if ok := requests.Validate(c, &request, requests.ResetByEmail); !ok {
		return
	}

	//更新密码
	userModel := user.GetByEmail(request.Email)
	if userModel.ID == 0 {
		response.Abort404(c, "不存在的用户")
	} else {
		userModel.Password = request.Password
		userModel.Save()

		response.Success(c)
	}
}
